unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, AdvEdit, AdvEdBtn, ExtCtrls, Db, ADODB, DBAdvSp, Mask, AdvSpin,
  HiLabel, Buttons, AdvPanel, MonthCalDialog, TWMonthCalendar, SapGrButton, GrupaSapGrButton;

type

  TForm1 = class(TForm)
    Conn: TADOConnection;
    wRegion: TADOQuery;
    cRegion1: TADOQuery;
    DataSource1: TDataSource;
    wHandlowiec: TADOQuery;
    wKlient: TADOQuery;
    Panel1: TPanel;
    Panel2: TPanel;
    AdvEditBtn1: TAdvEditBtn;
    Button1: TButton;
    AdvPanelStyler1: TAdvPanelStyler;
    Button2: TButton;
    MonthCalDialog1: TMonthCalDialog;
    Button3: TButton;
    Button4: TButton;
    procedure Panel1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure Panel1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure Panel2DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure Panel2DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure AdvPanel1Close(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    GrB1 : TGrupaSapGrButton;
    GrB2 : TGrupaSapGrButton;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Panel1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := (Source is TAdvEditBtn) or (Source is TSapGrButton);
end;

procedure TForm1.Panel1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  i, k : integer;
  Sap :  TSapGrButton;
begin
  if (Source is TAdvEditBtn) then
    (Source as TAdvEditBtn).Parent := Panel1
  else
  if (Source is TSapGrButton) then
  begin
    Sap := (Source as TSapGrButton);
    if Sap.Parent = Panel1 then
      Exit;
    Sap.Parent := Panel1;
    k := GrB2.GetItemIndex(Sap);
    GrB2.DeleteItem(k);
    GrB2.UluzItem;
    i := GrB1.ItemIndex(X, Y);
    //GrB1.AddItem(Sap);
    GrB1.InsertItem(Sap, i);
    GrB1.UluzItem;
  {  k := 0;
    for i := 0 to Self.ComponentCount-1 do
      if (Self.Components[i] is TSapGrButton) then
        if (Self.Components[i] as TSapGrButton).Parent = Panel1 then
          Inc(k);
    (Source as TSapGrButton).Parent := Panel1;
    (Source as TSapGrButton).Top := 5;
    (Source as TSapGrButton).Left := 5 + k*(89+5);}
  end;
end;


procedure TForm1.Panel2DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  i, k : integer;
  Sap : TSapGrButton;
begin
  if (Source is TAdvEditBtn) then
   (Source as TAdvEditBtn).Parent := Panel2
  else
  if (Source is TSapGrButton) then
  begin
    Sap := (Source as TSapGrButton);
    if Sap.Parent = Panel2 then
      Exit;
    Sap.Parent := Panel2;
    k := GrB1.GetItemIndex(Sap);
    GrB1.DeleteItem(k);
    GrB1.UluzItem;
    i := GrB2.ItemIndex(X, Y);
    GrB2.InsertItem(Sap, i);
    GrB2.UluzItem;
{    k := 0;
    for i := 0 to Self.ComponentCount-1 do
      if (Self.Components[i] is TSapGrButton) then
        if (Self.Components[i] as TSapGrButton).Parent = Panel2 then
          Inc(k);
    (Source as TSapGrButton).Parent := Panel2;
    (Source as TSapGrButton).Left := 5;
    (Source as TSapGrButton).Top := 3 + k*(17+3);}
  end;

end;

procedure TForm1.Panel2DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := (Source is TAdvEditBtn) or (Source is TSapGrButton);;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  AP : TAdvPanel;
begin
  AP := TAdvPanel.Create(Self);
//  AP.Parent := Panel4;
  Ap.Styler := AdvPanelStyler1;
  AP.OnClose := AdvPanel1Close;
end;

procedure TForm1.AdvPanel1Close(Sender: TObject);
begin
  Beep;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  MonthCalDialog1.Execute;
  Button2.Caption := IntToStr(MonthCalDialog1.Year)+'  '+IntToStr(MonthCalDialog1.Month);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  i : integer;
begin

  {for i := 0 to Self.ControlCount-1 do
    if (Self.Controls[i] is TSapGrButton) then
      if (Self.Controls[i] as TSapGrButton).Owner = (Self.Controls[i] as TSapGrButton).Parent then
         (Self.Controls[i] as TSapGrButton).Caption := 'Panel2';}
  for i := 0 to Self.ComponentCount-1 do
    if (Self.Components[i] is TSapGrButton) then
      if (Self.Components[i] as TSapGrButton).Parent = Panel2 then
         (Self.Components[i] as TSapGrButton).Caption := 'Panel2';

end;

procedure TForm1.Button4Click(Sender: TObject);
var
  i : integer;
begin

    for i := 0 to Panel2.ComponentCount - 1 do
      if (Panel1.Components[i] is TSapGrButton) then
          (Panel1.Components[i] as TSapGrButton).Caption := 'Panel1';
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  Sap : TSapGrButton;
begin
  GrB2 := TGrupaSapGrButton.Create;
  GrB2.ItemCount := 0;
  GrB2.ItemPoz := False;
  GrB2.Margin := 5;

  GrB1 := TGrupaSapGrButton.Create;
  GrB1.ItemCount := 0;
  GrB1.ItemPoz := True;
  GrB1.Margin := 5;


  Sap := TSapGrButton.Create(Self);
  Sap.Parent := Panel2;
  Sap.DragMode := dmAutomatic;
  Sap.Caption := '2222';
  GrB2.AddItem(Sap);

  Sap := TSapGrButton.Create(Self);
  Sap.Parent := Panel2;
  Sap.DragMode := dmAutomatic;
  Sap.Caption := '1111';
  GrB2.AddItem(Sap);

  Sap := TSapGrButton.Create(Self);
  Sap.Parent := Panel2;
  Sap.DragMode := dmAutomatic;
  Sap.Caption := '3333';
  GrB2.AddItem(Sap);

  GrB2.UluzItem;

  Sap := TSapGrButton.Create(Self);
  Sap.Parent := Panel1;
  Sap.DragMode := dmAutomatic;
  Sap.Caption := '4444';
  GrB1.AddItem(Sap);

  Sap := TSapGrButton.Create(Self);
  Sap.Parent := Panel1;
  Sap.DragMode := dmAutomatic;
  Sap.Caption := '5555';
  GrB1.AddItem(Sap);

  GrB1.UluzItem;
end;



procedure TForm1.FormDestroy(Sender: TObject);
begin
  GrB1.Free;
  GrB2.Free;
end;

end.
